import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import QtQuick.Dialogs 1.3
import com.RspInterface 1.0
import com.ComboData 1.0


//风险分段设置
Item {
    property var tabName
    //    property string listName: "RiskTryCalc"
    //    property bool bInital: false
    //    property bool bChangeWidth: false

    //    property string paramCalc

    //    property var nameWidth: 80
    //    property ComboData branchModel: new ComboData
    //g_MainQuo.getMyBranchs()
    property var valueWidth: 150
    property var midLine: 110
    property var kvSpace: 4
    //1 修改，2 新增，0 显示一条， -1 其它
    property var oper_flag: -1
    signal updateSetObj2
    onUpdateSetObj2: {
        var setobjName = gradeSetModel.get(gradeList.currentRow).set_obj
        if (calcObj2.find(setobjName) < 0)
            branchModel.append(setobjName, g_MainQuo.getBranchID(setobjName))
        calcObj2.currentIndex = calcObj2.find(setobjName)
        console.log("onUpdateSetObj2 " + calcObj2.currentIndex)
    }

    ColumnLayout {
        anchors.fill: parent
        RowLayout {
            anchors.top: parent.top
            anchors.topMargin: 5
            anchors.left: parent.left
            anchors.leftMargin: 20
            anchors.right: parent.right
            anchors.rightMargin: 20
            Layout.preferredHeight: 40
            Layout.alignment: Qt.AlignHCenter
            Text {
                text: "设置类型:"
                Layout.alignment: Qt.AlignHCenter
            }

            ComboBox {
                id: calcType
                Layout.preferredWidth: 90
                Layout.alignment: Qt.AlignHCenter
                model: ["所有类型", "客户", "客户级别", "代理机构", "账户类型"]

                onCurrentIndexChanged: {
                    if (currentIndex == 1) {
                        acct_No.visible = true
                        calcObj.visible = false
                        acct_No.enabled = true
                        acct_No.text = ""
                    } else if (0 == currentIndex) {
                        acct_No.visible = true
                        calcObj.visible = false
                        acct_No.text = "所有对象"
                        acct_No.enabled = false
                    } else {
                        acct_No.visible = false
                        calcObj.visible = true
                        //                        calcObj.model = null
                        if (2 == currentIndex) {
                            calcObj.model = g_MainQuo.getCustGradeByBranchs("")
                        } else if (3 == currentIndex) {
                            calcObj.model = g_MainQuo.getMyBranchs()
                        } else if (4 === currentIndex) {
                            calcObj.model = acctTypeModel2
                        }
                    }
                }
            }
            Item {
                Layout.preferredWidth: 25
            }
            Text {
                text: "设置对象:"
                Layout.alignment: Qt.AlignHCenter
            }
            TextField {
                Layout.preferredWidth: 150
                id: acct_No
                Layout.alignment: Qt.AlignHCenter
                //                text: acctNo
                placeholderText: "客户号"
            }
            ComboBox {
                id: calcObj
                Layout.alignment: Qt.AlignHCenter
                Layout.preferredWidth: 150
                //                model: calcValueDrop
            }

            Item {
                Layout.fillWidth: true
            }

            Button {
                text: "查询"
                Layout.alignment: Qt.AlignHCenter
                onClicked: {
                    queryGradeSetList()
                }
            }
            Item {
                Layout.preferredWidth: 40
            }
        }

        RowLayout {
            anchors.right: parent.right
            anchors.rightMargin: 20
            anchors.left: parent.left
            anchors.leftMargin: 20
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 40
            anchors.top: parent.top
            anchors.topMargin: 50
            //            Layout.fillHeight: true
            TableView {
                id: gradeList
                anchors.top: parent.top
                anchors.topMargin: 5
                anchors.left: parent.left
                //                anchors.leftMargin: 20
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 10
                anchors.right: ctrlSet.left
                anchors.rightMargin: 10
                model: gradeSetModel
                rowDelegate: Rectangle {
                    //                    color: "transparent"
                    color: styleData.selected ? selectColor : "white"
                    height: 64
                }

                TableViewColumn {
                    role: "set_type"
                    title: qsTr("设置类型")
                    width: gradeList.width * 0.15
                }

                TableViewColumn {
                    role: "set_obj"
                    title: qsTr("设置对象")
                    width: gradeList.width * 0.15
                }

                TableViewColumn {
                    role: "call_line"
                    title: qsTr("追保计算线")
                    width: gradeList.width * 0.15
                }

                TableViewColumn {
                    role: "force_line"
                    title: qsTr("相对强平线")
                    width: gradeList.width * 0.15
                }

                TableViewColumn {
                    role: "grade_set"
                    title: qsTr("风险分段")
                    width: gradeList.width * 0.4
                    delegate: gradeSubComponent
                }

                onClicked: {
                    //TreeView用currentIndex访问，TableView用currentRow
                    oper_flag = 0 //正常显示一条记录
                    //                    updateSetObj2()//提前插入要显示的数据，
                    callLineField.text = gradeSetModel.get(currentRow).call_line
                    forceLineField.text = gradeSetModel.get(
                                currentRow).force_line
                    var setobjName = gradeSetModel.get(currentRow).set_obj
                    var indexType = calcType2.find(gradeSetModel.get(
                                                       currentRow).set_type)
                    if (indexType === 3)
                        updateSetObj2() //必须提前插入要显示的数据，
                    calcType2.currentIndex = indexType

                    if (indexType === 3)
                        console.log("indexType setobjName")
                    else if (indexType !== 0) {
                        calcObj2.currentIndex = calcObj2.find(setobjName)
                    }

                    gradeSetLoader.item.forceLine = gradeSetModel.get(
                                currentRow).force_line
                }
            }

            GroupBox {
                id: ctrlSet
                anchors.top: parent.top
                anchors.topMargin: 5
                anchors.right: parent.right
                anchors.rightMargin: 20
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 10
                implicitWidth: 280
                implicitHeight: parent.height - 70
                title: "风险等级区间设定"

                Rectangle {
                    id: row1
                    anchors.top: parent.top
                    anchors.topMargin: 6
                    anchors.right: parent.right
                    anchors.left: parent.left
                    height: 24

                    Text {
                        id: setT1
                        text: "*设置类型:"
                        anchors.right: parent.right
                        anchors.rightMargin: kvSpace + parent.width - midLine
                        anchors.verticalCenter: parent.verticalCenter
                    }

                    ComboBox {
                        id: calcType2
                        enabled: oper_flag == 2 ? true : false
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.right: parent.right
                        anchors.rightMargin: 10
                        anchors.left: parent.left
                        anchors.leftMargin: midLine + kvSpace
                        model: ["所有类型", "客户", "客户级别", "代理机构", "账户类型"]

                        onCurrentIndexChanged: {
                            if (currentIndex == 1) {
                                acct_No2.text = ""
                            } else if (0 == currentIndex) {
                                acct_No2.text = "所有对象"
                            } else {
                                if (2 == currentIndex) {
                                    calcObj2.model = g_MainQuo.getCustGradeByBranchs(
                                                "", false)
                                } else if (3 == currentIndex) {
                                    //g_MainQuo.getMyBranchs()
                                    if (oper_flag != 0)
                                        calcObj2.model = g_MainQuo.getMyBranchs(
                                                    )
                                    else {
                                        calcObj2.model = branchModel
                                        console.log("click then branch model")
                                    }
                                } else if (4 === currentIndex) {
                                    calcObj2.model = acctTypeModel2
                                }

                                console.log("calcType2 IndexChanged oper_flag=" + oper_flag)
                                //                                if (oper_flag == 2)
                                //                                    calcObj2.enabled = true
                            }
                        }
                    }
                }
                Rectangle {
                    id: row2
                    anchors.top: row1.bottom
                    anchors.topMargin: 6
                    anchors.right: parent.right
                    anchors.left: parent.left
                    height: 24
                    Text {
                        id: setT2
                        text: "*设置对象:"
                        anchors.right: parent.right
                        anchors.rightMargin: kvSpace + parent.width - midLine
                        anchors.verticalCenter: parent.verticalCenter
                    }
                    TextField {
                        z: 1
                        id: acct_No2
                        enabled: (calcType2.currentIndex != 0
                                  && oper_flag == 2) ? true : false
                        visible: (calcType2.currentIndex == 0
                                  || calcType2.currentIndex == 1) ? true : false
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.right: parent.right
                        anchors.rightMargin: 10
                        anchors.left: parent.left
                        anchors.leftMargin: midLine + kvSpace
                        placeholderText: "客户号"
                        validator: RegExpValidator {
                            regExp: /[0-9]+/
                        }
                    }
                    ComboBox {
                        z: 3
                        id: calcObj2
                        anchors.right: parent.right
                        anchors.rightMargin: 10
                        anchors.left: parent.left
                        anchors.leftMargin: midLine + kvSpace
                        anchors.verticalCenter: parent.verticalCenter
                        width: valueWidth
                        enabled: oper_flag == 2 ? true : false
                        visible: (calcType2.currentIndex != 0
                                  && calcType2.currentIndex != 1) ? true : false
                        //                model: calcValueDrop
                    }
                }

                Rectangle {
                    id: row3
                    anchors.top: row2.bottom
                    anchors.topMargin: 6
                    anchors.right: parent.right
                    anchors.left: parent.left
                    height: 24
                    Text {
                        id: setT3
                        text: "*追保计算线:"
                        anchors.right: parent.right
                        anchors.rightMargin: kvSpace + parent.width - midLine
                        anchors.verticalCenter: parent.verticalCenter
                    }
                    TextField {
                        width: 50
                        id: callLineField
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.left: parent.left
                        anchors.leftMargin: midLine + kvSpace
                        enabled: (oper_flag == 1
                                  || oper_flag == 2) ? true : false
                        validator: RegExpValidator {
                            regExp: /[-][0-9.]+/
                        }
                    }
                    Text {
                        anchors.left: callLineField.right
                        anchors.leftMargin: 8
                        anchors.verticalCenter: parent.verticalCenter
                        text: "(-10000～0闭区间)"
                    }
                }
                Rectangle {
                    id: row4
                    anchors.top: row3.bottom
                    anchors.topMargin: 6
                    anchors.right: parent.right
                    anchors.left: parent.left
                    height: 24
                    Text {
                        id: setT4
                        text: "*相对强平线:"
                        anchors.right: parent.right
                        anchors.rightMargin: kvSpace + parent.width - midLine
                        anchors.verticalCenter: parent.verticalCenter
                    }
                    TextField {
                        width: 50
                        id: forceLineField
                        anchors.verticalCenter: parent.verticalCenter
                        anchors.left: parent.left
                        anchors.leftMargin: midLine + kvSpace
                        enabled: (oper_flag == 1
                                  || oper_flag == 2) ? true : false
                        validator: RegExpValidator {
                            regExp: /[0][.][0-9][0-9][0-9]/
                        }
                    }
                    Text {
                        anchors.left: forceLineField.right
                        anchors.leftMargin: 8
                        anchors.verticalCenter: parent.verticalCenter
                        text: "(0～1闭区间)"
                    }
                }

                GroupBox {
                    id: gradeSet
                    anchors.top: row4.bottom
                    anchors.topMargin: 10
                    anchors.right: parent.right
                    anchors.rightMargin: 20
                    anchors.left: parent.left
                    anchors.leftMargin: 20
                    implicitHeight: 100
                    implicitWidth: 250

                    Loader {
                        id: gradeSetLoader
                        anchors.fill: parent
                        sourceComponent: gradeSubComponent
                    }

                    Connections {
                        target: forceLineField
                        onTextChanged: gradeSetLoader.item.forceLine = forceLineField.text
                    }
                }

                Button {
                    id: addBtn
                    text: qsTr("新增")
                    anchors.left: parent.left
                    anchors.leftMargin: 15
                    anchors.bottom: parent.bottom
                    anchors.bottomMargin: 32
                    enabled: (oper_flag != 1 && oper_flag != 2) ? true : false
                    onClicked: {
                        oper_flag = 2

                        calcType2.currentIndex = 0
                        callLineField.text = ""
                        forceLineField.text = ""
                    }
                }

                Button {
                    id: modifyBtn
                    text: qsTr("修改")
                    anchors.left: addBtn.right
                    anchors.leftMargin: 10
                    anchors.bottom: parent.bottom
                    anchors.bottomMargin: 32
                    enabled: oper_flag == 0 ? true : false
                    onClicked: {
                        oper_flag = 1
                    }
                }

                Button {
                    id: delBtn
                    text: qsTr("删除")
                    anchors.left: modifyBtn.right
                    anchors.leftMargin: 10
                    anchors.bottom: parent.bottom
                    anchors.bottomMargin: 32
                    enabled: oper_flag == 0 ? true : false
                    onClicked: {
                        var req = "#oper_flag=3#obj_type=" + calcType2.currentIndex
                        if (2 == calcType2.currentIndex) {
                            req += "#obj_id=" + calcObj2.model.get(
                                        calcObj2.currentIndex)
                        } else if (1 == calcType2.currentIndex) {
                            req += "#obj_id=" + acct_No2.text
                        } else if (3 == calcType2.currentIndex) {
                            req += "#obj_id=" + calcObj2.model.get(
                                        calcObj2.currentIndex)
                        } else if (4 == calcType2.currentIndex) {
                            req += "#obj_id=" + calcObj2.model.get(
                                        calcObj2.currentIndex).acct_type
                        }

                        var rsp = g_MainQuo.operRiskDegree(req)
                        var msg = rsp.get("rsp_msg")
                        if (msg.length > 0) {
                            tipInput.text = msg
                            tipInput.open()
                            return
                        }

                        queryGradeSetList()
                    }
                }

                Button {
                    text: qsTr("保存")
                    anchors.left: addBtn.left
                    anchors.top: addBtn.bottom
                    anchors.topMargin: 10
                    enabled: (oper_flag == 1 || oper_flag == 2) ? true : false
                    onClicked: {
                        if (callLineField.text.length < 2) {
                            tipInput.text = "请填写追保计算线"
                            tipInput.open()
                            return
                        } else if (forceLineField.text.length < 3) {
                            tipInput.text = "请填写相对强平线"
                            tipInput.open()
                            return
                        } else if (1 == calcType2.currentIndex
                                   && acct_No2.text.length < 5) {
                            tipInput.text = "请填写客户号"
                            tipInput.open()
                            return
                        }

                        var req = "#oper_flag=" + oper_flag + "#obj_type=" + calcType2.currentIndex
                        if (2 == calcType2.currentIndex) {
                            req += "#obj_id=" + calcObj2.model.get(
                                        calcObj2.currentIndex)
                        } else if (1 == calcType2.currentIndex) {
                            req += "#obj_id=" + acct_No2.text
                        } else if (3 == calcType2.currentIndex) {
                            req += "#obj_id=" + calcObj2.model.get(
                                        calcObj2.currentIndex)
                        } else if (4 == calcType2.currentIndex) {
                            req += "#obj_id=" + calcObj2.model.get(
                                        calcObj2.currentIndex).acct_type
                        }

                        req += "#debt_call_value=" + callLineField.text
                        req += "#debt_exch_value=" + forceLineField.text
                        req += almGradeSet()
                        var rsp = g_MainQuo.operRiskDegree(req)
                        var msg = rsp.get("rsp_msg")
                        if (msg.length > 0) {
                            tipInput.text = msg
                            tipInput.open()
                            return
                        }

                        queryGradeSetList()
                    }
                }

                Button {
                    text: qsTr("取消")
                    anchors.left: modifyBtn.left
                    anchors.top: modifyBtn.bottom
                    anchors.topMargin: 10
                    enabled: (oper_flag == 1 || oper_flag == 2) ? true : false
                    onClicked: {
                        oper_flag = -1
                    }
                }
            }
        }
    }

    Button {
        text: "关闭"
        anchors.right: parent.right
        anchors.rightMargin: 25
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        onClicked: closeTab(tabName)
    }

    Component {
        id: gradeSubComponent
        ColumnLayout {
            //            anchors.fill: parent
            property var forceLine: (typeof styleData !== "undefined") ? styleData.value : ""
            property var hei: (typeof styleData !== "undefined") ? 12 : 18
            NameValue {
                name: qsTr("正常") + colon
                value: "-10000 ～ 0"
                Layout.alignment: Qt.AlignCenter
                height: hei
                selected: (typeof styleData !== "undefined") ? styleData.selected : false
            }
            NameValue {
                name: qsTr("追保") + colon
                value: "0 ～ " + forceLine
                Layout.alignment: Qt.AlignCenter
                height: hei
                selected: (typeof styleData !== "undefined") ? styleData.selected : false
            }
            NameValue {
                name: qsTr("相对强平") + colon
                value: forceLine + " ～ 1"
                Layout.alignment: Qt.AlignCenter
                height: hei
                selected: (typeof styleData !== "undefined") ? styleData.selected : false
            }
            NameValue {
                name: qsTr("绝对强平") + colon
                value: "1 ～ 100"
                Layout.alignment: Qt.AlignCenter
                height: hei
                selected: (typeof styleData !== "undefined") ? styleData.selected : false
            }
        }
    }

    ListModel {
        id: gradeSetModel
    }

    ComboData {
        id: branchModel
    }

    //#debt_call_value=-0.91#debt_exch_value=0.81#obj_content=11｜-10000｜0｜∧21｜0｜0.81｜∧31｜0.81｜1｜∧41｜1｜100｜∧#obj_id=2#obj_type=4#oper_flag=2#
    function almGradeSet() {
        var obj_content = "#obj_content=11｜-10000｜0｜∧21｜0｜" + forceLineField.text
                + "｜∧31｜" + forceLineField.text + "｜1｜∧41｜1｜100｜∧"
        return obj_content
    }

    function queryGradeSetList() {
        var req = "#obj_type=" + calcType.currentIndex
        if (2 == calcType.currentIndex) {
            if (calcObj2.currentIndex != 0)
                req += "#obj_id=" + calcObj.model.get(calcObj.currentIndex)
        } else if (1 == calcType.currentIndex) {
            req += "#obj_id=" + acct_No.text
        } else if (3 == calcType.currentIndex) {
            req += "#obj_id=" + calcObj.model.get(calcObj.currentIndex)
        } else if (4 == calcType.currentIndex) {
            req += "#obj_id=" + calcObj.model.get(
                        calcObj.currentIndex).acct_type
        }

        var rsp = g_MainQuo.operRiskDegree(req)
        gradeSetModel.clear()
        var almName = "alm_result"
        var nSize = rsp.getAlmSize(almName)
        for (var i = 0; i < nSize; ++i) {
            var setType = rsp.getAlmString(almName, i, 0)
            var setObj = rsp.getAlmString(almName, i, 1)
            if (setType === "0")
                setObj = "所有对象"
            else if (setType === "2")
                setObj = g_MainQuo.getCustGradeName(setObj)
            else if (setType === "3")
                setObj = g_MainQuo.getBranchName(setObj)
            else if (setType === "4")
                setObj = setObj === "2" ? "法人" : "个人"

            setType = calcType.textAt(parseInt(setType))

            //                        if (setType === "0")
            //                            setType = "所有类型"
            gradeSetModel.append({
                                     set_type: setType,
                                     set_obj: setObj,
                                     call_line: rsp.getAlmString(almName, i, 3),
                                     force_line: rsp.getAlmString(almName, i,
                                                                  2),
                                     grade_set: rsp.getAlmString(almName, i, 2)
                                 })
        }

        oper_flag = -1
    }
}
